import { _decorator, Color, Component, director, find, Label, Node, Vec3 } from 'cc';
import { Global } from '../global';
const { ccclass, property } = _decorator;

@ccclass('nickName')
export class nickName extends Component {
    // 名字
    @property(Label)
    nickNameNode: Label = null;

    private _sex: number = 0;

    // 随机生成名字
    randomName() {
        // 随机生成姓氏
        let lastName: string[] = ['赵', '钱', '孙', '李', '周', '吴', '郑', '王'];
        let lastNameIndex: number = Math.floor(Math.random() * lastName.length);
        let firstName: string[] = ['伟龙', '伟强', '伟杰', '伟华', '李子', '运屈', '佳乐', '嘉宾', '菜鸡'];
        let firstNameIndex: number = Math.floor(Math.random() * firstName.length);
        return lastName[lastNameIndex] + firstName[firstNameIndex];
    }

    // 点击设置名字
    onClick() {
        this.nickNameNode.string = this.randomName();
    }

    // 选择性别
    changePic(event, customData: Node) {
        let target = event.currentTarget; // 获取当前节点
        this._sex = target.name == 'woman' ? 2 : 1;
        target.scale = new Vec3(1.2, 1.2, 1.2); // 放大
        target.color = new Color(255, 0, 0); // 改变颜色
        let otherNode = find('Canvas/bg/' + customData);
        otherNode.scale = new Vec3(0.8, 0.8, 0.8); // 改变其他节点大小
        this.onClick();
    }

    // 创建角色
    createRole() {
        if (this.nickNameNode.string === '' && this._sex === 0) return;
        let account = Global.UsrMg.account;
        let sign = Global.UsrMg.sign;
        let username = this.nickNameNode.string;
        let sex = this._sex;
        let params = {
            account,
            sign,
            username,
            sex
        }
        // 发送请求，创建角色
        Global.http.sendRequest('/createRole','POST',params, (data) => {
            if (data.code === 1) {
                console.log(data.msg);
                return;
            }
            Global.UsrMg.username = data.username;
            Global.UsrMg.sex = data.sex;
            director.loadScene('game');
        })
    }
}


